import os
import unittest
import nose
from limber import componentfinder
from limber import abilities
from mocks import *
from tests import tools

def setup_module():
    set_mockdir(0)

def testInstantiate():
    parent = MockApplication()
    userdir = parent.userdir_path()
    systemdir = parent.systemdir_path()
    finder = componentfinder.ComponentFinder(userdir, systemdir)


class TestComponentFinder(unittest.TestCase):

    def setUp(self):
        self.parent = MockApplication()
        userdir = self.parent.userdir_path()
        systemdir = self.parent.systemdir_path()
        self.finder = componentfinder.ComponentFinder(userdir, systemdir)

    def testGetPaths(self):
        usrdir = os.path.join(self.parent.userdir_path(), 'Components')
        sysdir = os.path.join(self.parent.systemdir_path(),'Components')

        usr,system = self.finder.get_paths()
        self.assertEqual(usr, usrdir, 'Wrong user directory.')
        self.assertEqual(system, sysdir, 'Wrong system directory.')

        root = '/home/someone/.inside/hidden/directory'
        usrdir2 = os.path.join(root, 'Components')

        usr,system = self.finder.get_paths(userdir=root)
        self.assertEqual(usr, usrdir2, 'Wrong user directory when changing root.')
        self.assertEqual(system, sysdir, 'Wrong system directory when changing '
                                         'the USER.')

        sysdir2 = os.path.join(root, 'Components')
        usr,system = self.finder.get_paths(systemdir=root)
        self.assertEqual(usr, usrdir, 'Wrong user directory when changing '
                                      'the SYSTEM root.')
        self.assertEqual(system, sysdir2, 'Wrong system directory '
                                          'when changing root.')
    
    @tools.with_path(get_userdir)
    def testFindComponentsInfos(self):
        usrdir, sysdir = self.finder.get_paths()
        right_infos = sorted(comp.__dict__ for comp in mock_component_infos(0))
        usr_infos = sorted(comp.__dict__
                           for comp in self.finder.find_components_infos(usrdir))
        self.assertEqual(usr_infos, right_infos)
    
    @tools.with_path(get_userdir)
    def testLoadComponent(self):
        usrdir, sysdir = self.finder.get_paths()
        
        right_infos = mock_component_infos(0)
        component = self.finder.load_component(right_infos[0])
        self.assertEqual(component, mock_components(0)[0])
    
    @tools.with_path(get_userdir)
    def testFindComponents(self):
        usrdir, sysdir = self.finder.get_paths()
        
        right_components = sorted(mock_components(0))
        usr_components = sorted(self.finder.find_components(usrdir))
        self.assertEqual(usr_components, right_components,
                         'Wrong components found.')
    
    @tools.with_path(get_systemdir)
    def testFindComponentsAndAbilities(self):
        usrdir, sysdir = self.finder.get_paths()

        components_and_abilities = mock_components_and_abilities(0)
        got_comps_and_abils = self.finder.find_components_and_abilities(sysdir)

        for pos, comp_and_abil in enumerate(got_comps_and_abils):
            self.assertEqual(comp_and_abil, components_and_abilities[pos])

    def testGetAbilities(self):
        abils_1 = ('TEXT','PLAIN_TEXT')
        right_1 = (
            abilities.TEXT,
            abilities.PLAIN_TEXT)

        self.assertEqual(self.finder.get_abilities(abils_1), right_1)

        abils_2 = ('TEXT','HTML_TEXT','VIEW')
        right_2 = (
            abilities.TEXT,
            abilities.HTML_TEXT,
            abilities.VIEW)

        self.assertEqual(self.finder.get_abilities(abils_2), right_2)

        abils_3 = ('TEXT', 'WINGS')
        right_3 = (
            abilities.TEXT,
            1 << len(abilities.ABILITIES))

        self.assertEqual(self.finder.get_abilities(abils_3), right_3)

        abils_4 = ('TEXT', 'INVALID-ability_name')
        self.assertRaises(componentfinder.ComponentFinderError,
                          self.finder.get_abilities, abils_4)
